// Copyright (C) 1997-2002 Alias|Wavefront,
// a division of Silicon Graphics Limited.
//
// The information in this file is provided for the exclusive use of the
// licensees of Alias|Wavefront.  Such users have the right to use, modify,
// and incorporate this code into other products for purposes authorized
// by the Alias|Wavefront license agreement, without fee.
//
// ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
// INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
// EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
// DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
//
//
//  Alias|Wavefront Script File
//
//  Creation Date:	June 3, 1997
//  Author:		sw
//
//  Procedure Name:
//	AEnormalConstraintTemplate
//
//  Description Name;
//	Creates the attribute editor controls for the normalConstraint Node
//
//  Input Value:
//	nodeName 
//
//  Output Value:
//	None
// 

global string $gAEnormalConstraintWorldUpWidgets[];	// hold info plus widgets

global proc AEenableRestNormalCallback( string $nodeName )
{
	int $enable = `getAttr ($nodeName+".enableRestPosition")`;
	editorTemplate -dimControl $nodeName "restRotate" (! $enable);	
}


global proc AEnormalConstraintTemplate ( string $nodeName )
{
	editorTemplate -beginScrollLayout;
		
		// include/call base class/node attributes
		AEtransformMain $nodeName;
	
		editorTemplate -beginLayout "Normal Constraint Attributes" -collapse 0;
			editorTemplate -addControl "aimVector";
			editorTemplate -addControl "upVector";
			editorTemplate -addControl "worldUpType" 
					"AEnormalConstraintWorldUpControls";
			editorTemplate -addControl "worldUpVector"; 
			editorTemplate -callCustom
				"AEnormalConstraintWorldUpObjectNew"
				"AEnormalConstraintWorldUpObjectReplace"
				"worldUpMatrix";

			editorTemplate -addSeparator;

			editorTemplate -addControl "constraintRotate";
			editorTemplate -addControl "constraintVector";
			editorTemplate -addControl "enableRestPosition" "AEenableRestNormalCallback";			
			editorTemplate -addControl "restRotate";			

		editorTemplate -endLayout;
	
		// include/call base class/node attributes
		AEtransformNoScroll $nodeName;

		// supressed attributes
		editorTemplate -suppress "constraintRotateOrder";
		editorTemplate -suppress "constraintTranslate";
		editorTemplate -suppress "constraintRotatePivot";
		editorTemplate -suppress "constraintJointOrient";
		editorTemplate -suppress "constraintParentInverseMatrix";
		editorTemplate -suppress "worldUpType";
//		editorTemplate -suppress "worldUpVector";
		editorTemplate -suppress "worldUpMatrix";
		editorTemplate -suppress "target";
		editorTemplate -suppress "constraintRotateTranslate";

	editorTemplate -addExtraControls;
	editorTemplate -endScrollLayout;
}

proc setStateOfWorldUpObjectControls( string $nodeName, int $state )
{
	global string $gAEnormalConstraintWorldUpWidgets[];

	// Update the world up object text field for all attribute
	// editors looking at this node

	int $i;
    for ($i = 0; $i < size($gAEnormalConstraintWorldUpWidgets); $i += 3 )
	{
        if ( $nodeName == $gAEnormalConstraintWorldUpWidgets[$i] ) 
		{
			string $worldUpObjectControl = $gAEnormalConstraintWorldUpWidgets[$i+2];
			if ( `textFieldGrp -exists $worldUpObjectControl` )
			{
				textFieldGrp -e -enable $state $worldUpObjectControl;
			}
        }
    }
}

// This proc will dim/undim the world up controls as required
global proc AEnormalConstraintWorldUpControls(string $nodeName)
{
	$nodeAttr = $nodeName +".worldUpType";
	int $value = `getAttr $nodeAttr`;
	switch ($value) 
	{
	case 0: // Scene Up
	case 4: // None
		editorTemplate -dimControl $nodeName "worldUpVector" true;
		setStateOfWorldUpObjectControls( $nodeName, false );
		break;
	case 1: // Object Up
		editorTemplate -dimControl $nodeName "worldUpVector" true;
		setStateOfWorldUpObjectControls( $nodeName, true );
		break;
	case 2: // Object Rotation Up
		editorTemplate -dimControl $nodeName "worldUpVector" false;
		setStateOfWorldUpObjectControls( $nodeName, true );
		break;
	case 3: // Vector
		editorTemplate -dimControl $nodeName "worldUpVector" false;
		setStateOfWorldUpObjectControls( $nodeName, false );
		break;
	default:
		break;
	}
}


// This proc gets called by the UI widget for the
// worldUpMatrix attribute
//
global proc AEnormalConstraintWorldUpObjectControlProc(string $nodeName,
													string $widget)
{
	// Get the current value of the widget

	string $value = `textFieldGrp -q -text $widget`;

	// setAttr just isn't sufficient for worldUpMatrix

	normalConstraint -edit -worldUpObject $value $nodeName;
}

// This proc gets called by the scriptJob which is watching the
// worldUpMatrix attribute
//
global proc AEnormalConstraintWorldUpObjectScriptJobProc(string $nodeName,
													  string $widget)
{
	textFieldGrp -e
		-text `normalConstraint -q -worldUpObject $nodeName`
		$widget;
}

// Local proc shared by AEnormalConstraintWorldUpObjectNew() and AEnormalConstraintWorldUpObjectReplace()
proc updateWorldUpObject( string $nodeName, string $plug, string $worldUpObjectControl )
{
	// Initialize/update the textFieldGrp

	string $cmd1 = "AEnormalConstraintWorldUpObjectControlProc "+$nodeName+" "+$worldUpObjectControl;
	textFieldGrp -e
		-text `normalConstraint -q -worldUpObject $nodeName`
		-cc $cmd1
		$worldUpObjectControl;
	
	// Setup/update the scriptJob callback mechanism

	string $cmd2 = "AEnormalConstraintWorldUpObjectScriptJobProc "+$nodeName+" "+$worldUpObjectControl;
	scriptJob -rp -p $worldUpObjectControl -ac $plug $cmd2;
}

// The custom creation proc for the worldUpMatrix attribute
//
global proc AEnormalConstraintWorldUpObjectNew( string $plug )
{
	global string $gAEnormalConstraintWorldUpWidgets[];

	// Get the node name

	string $buffer[];
	tokenize($plug,".|",$buffer);
	string $nodeName = $buffer[0];

	// Save the node name and the parent info in the global
	// string array

	int $numNormalConstraintAEs = size($gAEnormalConstraintWorldUpWidgets);
	$gAEnormalConstraintWorldUpWidgets[$numNormalConstraintAEs] = $nodeName;
	$gAEnormalConstraintWorldUpWidgets[$numNormalConstraintAEs+1] = `setParent -q`;

	// Create the textFieldGrp

	setUITemplate -pst attributeEditorTemplate;

	string $worldUpObjectControl =
		`textFieldGrp -l "World Up Object" AEnormalConstraintWorldUpObjectField`;

	// Save the control for possible later reuse
	$gAEnormalConstraintWorldUpWidgets[$numNormalConstraintAEs+2] = $worldUpObjectControl;

	setUITemplate -ppt;

	updateWorldUpObject( $nodeName, $plug, $worldUpObjectControl );
}

global proc AEnormalConstraintWorldUpObjectReplace( string $plug )
{
	global string $gAEnormalConstraintWorldUpWidgets[];

	// Get the node name

	string $buffer[];
	tokenize($plug,".|",$buffer);
	string $nodeName = $buffer[0];

	// Reuse the existing control

	string $curParent = `setParent -q`;
	int $index = -1;
	int $i;
    for ( $i = 0; $i < size($gAEnormalConstraintWorldUpWidgets); $i += 3 ) 
	{
        if ( $curParent == $gAEnormalConstraintWorldUpWidgets[$i+1] )
		{
            $index = $i;
            break;
        }
    }
    string $worldUpObjectControl = "AEnormalConstraintWorldUpObjectField";
    if ($index != -1)
	{
        $gAEnormalConstraintWorldUpWidgets[$index] = $nodeName;
        $worldUpObjectControl = $gAEnormalConstraintWorldUpWidgets[$index+2];
    }

	updateWorldUpObject( $nodeName, $plug, $worldUpObjectControl );
}
